home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.03 Mar 90 / Mouse Source / TrackAutoScroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-22  |  3.1 KB  |  132 lines  |  [TEXT/KAHL]

  1. /*                                        TrackAutoScroll.c                                    */
  2. /*
  3.  * Copyright © 1989 Martin Minow. All rights reserved.
  4.  *
  5.  * Manage automatic scrolling.
  6.  *
  7.  * void
  8.  * TrackSelView(track_handle)
  9.  * TrackHandle    track_handle;
  10.  *
  11.  * If automatic scrolling has been enabled, make sure the
  12.  * start of the selection range is visible, scrolling
  13.  * it into view if necessary.  If automatic scrolling
  14.  * is disabled, TrackSelView does nothing.
  15.  *
  16.  * void
  17.  * TrackAutoView(auto, track_handle)
  18.  * Boolean            auto;
  19.  * TrackHandle    track_handle;
  20.  *
  21.  * Enable or disable automatic scrolling.
  22.  */
  23. #include    "TrackEdit.h"
  24. #define TR    (*tr)
  25. /*
  26.  * Calculate the width or height of a rect.
  27.  */
  28. #define    width(r) ((r).right - (r).left)
  29. #define    height(r) ((r).bottom - (r).top)
  30.  
  31. /*
  32.  * Scroll the selection start into view.
  33.  */
  34. void
  35. TrackSelView(track_handle)
  36. TrackHandle    track_handle;
  37. {
  38.         register TrackPtr    tr;
  39.         _Track_state            state;
  40.         LONGINT                        hpixel, vpixel;
  41.         LONGINT                        hdelta, vdelta;
  42.         
  43.         tr = _Track_lock(track_handle, &state);
  44.         if (!_Track_is_set(tr, _Track_do_autoscroll))
  45.             return;
  46.         TrackGetPoint(
  47.             TR.selStart, track_handle, &hpixel, &vpixel);
  48.         vpixel -= TR.lineHeight;
  49.         hdelta = vdelta = 0;
  50.         if (vpixel < (LONGINT) TR.viewRect.top
  51.          || vpixel > (LONGINT) TR.viewRect.bottom)
  52.              vdelta = vpixel - TR.viewRect.top;
  53.         if (hpixel < (LONGINT) TR.viewRect.left
  54.          || hpixel > (LONGINT) TR.viewRect.right)
  55.              hdelta = hpixel - TR.viewRect.left;
  56.         if (hdelta != 0 || vdelta != 0)
  57.             _Track_do_scroll(tr, hdelta, vdelta);
  58.         _Track_unlock(&state);
  59. }
  60.  
  61. /*
  62.  * Turn on/off automatic scrolling.
  63.  */
  64. void
  65. TrackAutoView(enable, track_handle)
  66. Boolean            enable;
  67. TrackHandle    track_handle;
  68. {
  69.         if (enable)
  70.             _Track_set((*track_handle), _Track_do_autoscroll);
  71.         else {
  72.             _Track_clear((*track_handle), _Track_do_autoscroll);
  73.         }
  74. }
  75.  
  76. /*
  77.  * _Track_autoscroll()
  78.  * Make sure the mouse is within the viewRect, scrolling
  79.  * the text if necessary.  This should "pin" the text
  80.  * so it doesn't scroll out of the window.
  81.  */
  82. void
  83. _Track_autoscroll(tr, mousep)
  84. register TrackPtr    tr;
  85. register Point        *mousep;
  86. {
  87.         LONGINT                deltah, deltav;
  88.         LONGINT                max_horiz, max_vert;
  89.         
  90.         if (!_Track_is_set(tr, _Track_do_autoscroll))
  91.             return;
  92.         deltah = 0;
  93.         deltav = 0;
  94.         if (mousep->v < TR.viewRect.top
  95.          && TR.topPixel > 0) {
  96.             --deltav;
  97.             mousep->v = TR.viewRect.top;
  98.         }
  99.         else if (mousep->v > TR.viewRect.bottom) {
  100.             max_vert = (TR.nLines * TR.lineHeight)
  101.                              - height(TR.viewRect);
  102.             if (TR.topPixel < max_vert) {
  103.                 ++deltav;
  104.                 mousep->v = TR.viewRect.bottom;
  105.             }
  106.         }
  107.         if (mousep->h < TR.viewRect.left
  108.          && TR.leftPixel > 0) {
  109.             --deltah;
  110.             mousep->h = TR.viewRect.left;
  111.         }
  112.         else if (mousep->h > TR.viewRect.right) {
  113.             max_horiz = TR.lineWidth - width(TR.viewRect);
  114.             if (TR.crOnly < 0 || TR.leftPixel < max_horiz) {
  115.                 ++deltah;
  116.                 mousep->h = TR.viewRect.right;
  117.             }
  118.         }
  119.         /*
  120.          * Each pass through autoscroll moves the window
  121.          * a few pixels at a time -- this slows things down
  122.          * so the user can stop before the text disappears.
  123.          */
  124.         if (deltav != 0 || deltah != 0) {
  125.             _Track_do_scroll(
  126.                 tr,
  127.                 deltah * TR.lineHeight,
  128.                 deltav * TR.lineHeight
  129.             );
  130.         }
  131. }
  132.